<?php

namespace App\Http\Controllers\Admin;

use App\Models\Trade;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class TradeController extends BaseController
{
    /**
     *
     * @throws \Exception
     */
    public function index(){

        $this->assign([
            'subject' => '',
            'payer_name' => '',
            'payee_name' => '',
            'pay_status' => '',
            'min_amount' => '',
            'max_amount' => '',
            'pay_type' => '',
            'trade_no' => '',
            'time_begin' => '',
            'time_end' => ''
        ]);

        $queryParams = [];
        $query = Trade::where([]);
        $subject = $this->request->get('subject');
        if ($subject) {
            $queryParams['subject'] = $subject;
            $query = $query->where('subject', 'LIKE', "%$subject%");
        }

        $payer_name = $this->request->get('payer_name');
        if ($payer_name) {
            $queryParams['payer_name'] = $payer_name;
            $query = $query->where('payer_name', $payer_name);
        }

        $payee_name = $this->request->get('payee_name');
        if ($payee_name) {
            $queryParams['payee_name'] = $payee_name;
            $query = $query->where('payee_name', $payee_name);
        }

        $pay_status = $this->request->get('pay_status');
        if ($pay_status) {
            $queryParams['pay_status'] = $pay_status;
            if ($pay_status == 'PAID'){
                $query = $query->where('pay_status', 1);
            }
            if ($pay_status == 'UNPAID'){
                $query = $query->where('pay_status', 0);
            }
        }

        $min_amount = $this->request->get('min_amount');
        if ($min_amount) {
            $queryParams['min_amount'] = $min_amount;
            $query = $query->where('total_amount', '>', floatval($min_amount));
        }

        $max_amount = $this->request->get('max_amount');
        if ($max_amount) {
            $queryParams['max_amount'] = $max_amount;
            $query = $query->where('total_amount', '<', floatval($max_amount));
        }

        $pay_type = $this->request->get('pay_type');
        if ($pay_type) {
            $queryParams['pay_type'] = $pay_type;
            $query = $query->where('pay_type', $pay_type);
        }

        $trade_no = $this->request->get('trade_no');
        if ($trade_no) {
            $queryParams['trade_no'] = $trade_no;
            $query = $query->where('trade_no', $trade_no);
        }

        $time_begin = $this->request->get('time_begin');
        if ($time_begin) {
            $queryParams['time_begin'] = $time_begin;
            $query = $query->where('created_at', '>', strtotime($time_begin));
        }

        $time_end = $this->request->get('time_end');
        if ($time_end) {
            $queryParams['time_end'] = $time_end;
            $query = $query->where('created_at', '<', strtotime($time_end));
        }

        $this->assign($queryParams);

        $trades = $query->orderByDesc('trade_id')->paginate(20);
        $this->assign([
            'trades'=>$trades,
            'pagination'=>$trades->appends($queryParams)->render(),
            'payStatus'=>trans('mall.pay_status')
        ]);

        return $this->view('admin.trade.trade');
    }

    public function export(){

        $trades = [
            [
                '交易号',
                '名称',
                '内容',
                '金额',
                '付款方',
                '收款方',
                '支付状态',
                '交易时间'
            ]
        ];

        $query = Trade::where([]);
        $subject = $this->request->get('subject');
        if ($subject) {
            $query = $query->where('subject', 'LIKE', "%$subject%");
        }

        $payer_name = $this->request->get('payer_name');
        if ($payer_name) {
            $query = $query->where('payer_name', $payer_name);
        }

        $payee_name = $this->request->get('payee_name');
        if ($payee_name) {
            $query = $query->where('payee_name', $payee_name);
        }

        $pay_status = $this->request->get('pay_status');
        if ($pay_status) {
            if ($pay_status == 'PAID'){
                $query = $query->where('pay_status', 1);
            }
            if ($pay_status == 'UNPAID'){
                $query = $query->where('pay_status', 0);
            }
        }

        $min_amount = $this->request->get('min_amount');
        if ($min_amount) {
            $query = $query->where('total_amount', '>', floatval($min_amount));
        }

        $max_amount = $this->request->get('max_amount');
        if ($max_amount) {
            $query = $query->where('total_amount', '<', floatval($max_amount));
        }

        $pay_type = $this->request->get('pay_type');
        if ($pay_type) {
            $query = $query->where('pay_type', $pay_type);
        }

        $trade_no = $this->request->get('trade_no');
        if ($trade_no) {
            $query = $query->where('trade_no', $trade_no);
        }

        $time_begin = $this->request->get('time_begin');
        if ($time_begin) {
            $query = $query->where('created_at', '>', strtotime($time_begin));
        }

        $time_end = $this->request->get('time_end');
        if ($time_end) {
            $query = $query->where('created_at', '<', strtotime($time_end));
        }

        $query->orderByDesc('trade_id')->limit(10000)->get()->map(function (Trade $trade) use (&$trades){
            $trades[] = [
                $trade->trade_no,
                $trade->subject,
                $trade->body,
                $trade->total_amount,
                $trade->payer_name,
                $trade->payee_name,
                $trade->pay_status ? '已支付' : '未支付',
                @date('Y-m-d H:i:s', $trade->created_at)
            ];
        });

        $collection = new Collection($trades);
        return $collection->downloadExcel(
            'tradeTable.xls',
            'Xls',
            false
        );
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchDelete(Request $request)
    {
        $trades = $request->input('trades');
        if ($trades)
        {
            Trade::whereIn('trade_id', $trades)->get()->map(function (Trade $trade){
                $trade->delete();
            });
        }

        return ajaxReturn();
    }
}
